{include "INC_header.html" title="配置"}
<div id="app-outlet" class="padding">
    <!-- ant-design 应用需要配置 -->
    <a-config-provider :locale="configs.locale" :theme="configs.theme">
        <!-- 系统配置 -->
        <div class="ui form">
            <div class="field">
                <div class="label">控制面板默认页面</div>
                <a-input type="text" style="width: 400px;" v-model:value="config.adminHome" placeholder="请填写完整的URL地址"></a-input>
            </div>
            <div class="field">
                <div class="label">禁止某些IP请求</div>
                <a-textarea rows="5" style="width: 600px;" v-model:value="config.disableIP" placeholder="支持填写*，英文逗号分隔"></a-textarea>
            </div>
            <div class="field">
                <div class="label">PHP CLI 名称</div>
                <a-input style="width: 600px;" v-model:value="config.php" placeholder="默认php，请注意版本必须是8.0+"></a-input>
            </div>
            <div class="field">
                <div class="label">Composer CLI 名称</div>
                <a-input style="width: 600px;" v-model:value="config.composer" placeholder="默认composer，请注意版本必须是2.0+"></a-input>
            </div>
            <div class="field">
                <div class="label">应用商店</div>
                <a-radio-group v-model:value="config.store.enable">
                    <a-radio :value="true">打开</a-radio>
                    <a-radio :value="false">关闭</a-radio>
                </a-radio-group>
            </div>
            <div class="field" v-if="config.store.enable">
                <div class="label">应用商店地址</div>
                <a-input type="text" style="width: 600px;" v-model:value="config.store.url" placeholder="请填写完整的URL地址"></a-input>
            </div>
            <div class="field" v-if="config.store.enable">
                <div class="label">应用商店授权密钥(<a :href="config.store.url + '/auth'" target="_blank">申请密钥</a>)</div>
                <a-input type="text" style="width: 600px;" v-model:value="config.store.key" placeholder="填写授权密钥"></a-input>
            </div>
            <div class="field">
                <a-button size="large" type="primary" :loadnig="saveLoading" @click="save">保存</a-button>
            </div>
        </div>
    </a-config-provider>
</div>
<script>
    //vue app
    const { createApp, defineComponent, ref } = Vue;
    const { message } = antd;
    const app = createApp({
        data() {
            return {
                //主题配置
                configs: appProviderConfig,
                //数据
                config: {
                    adminHome: '',
                    store: {
                        enable: true,
                        url: '',
                        key: ''
                    },
                    disableIP: ''
                },
                php: 'php',
                name: 'admin',
                saveLoading: false
            }
        },
        mounted() {
            //挂载成功
            const hide = message.loading("正在加载配置...");
            let self = this;
            axios.get("{url url='/admin/api/config'}", {
                params: {
                    name: self.name
                }
            }).then(({ data }) => {
                hide();
                if (data.error === 0) {
                    if (self.name === "assets") {
                        //需要将配置转换为数组
                        let list = [];
                        for(let f in data.data) {
                            list.push({
                                suffix: f,
                                contentType: data.data[f]
                            });
                        }
                        self.config = list;
                    } else {
                        self.config = data.data;
                    }
                } else {
                    message.error(data.message);
                }
            }).catch(() => {
                hide();
                message.error("服务端繁忙");
            });
        },
        methods: {
            //方法
            save: function () {
                if (this.saveLoading) {
                    return;
                }
                this.saveLoading = true;
                let self = this;
                axios.post("{url url='/admin/api/config/save'}", {
                    //原文提交过去
                    config: this.config
                }).then(({ data }) => {
                    self.saveLoading = false;
                    if (data.error === 0) {
                        message.success(data.message);
                    } else {
                        message.error(data.message);
                    }
                }).catch(() => {
                    self.saveLoading = false;
                    message.error("服务端繁忙");
                });
            }
        }
    }).use(antd);
    //挂载
    const vm = app.mount("#app-outlet");
</script>
{include "INC_footer.html"}